package master.system.function;

import java.util.List;
import java.util.Map;

import master.support.DAOSupport;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.stereotype.Service;

/**
 * Service to operate system access node, including module&function
 *
 * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
 */
@Service
public class FunctionService {

	@Autowired
	DAOSupport daoSupport;

	/**
	 * 获取指定模块
	 * @param id
	 * @return
	 */
	public Module getModuleById(long id) {
		return daoSupport.getJdbcTemplate().queryForObject("select * from sys_module where id=?",
				new BeanPropertyRowMapper<Module>(Module.class), id);
	}

	/**
	 * 获取所有模块
	 * @return
	 */
	public List<Module> getAllModules(){
		return daoSupport.getJdbcTemplate().query("select * from sys_module",
				new BeanPropertyRowMapper<Module>(Module.class));
	}

	/**
	 * 保存的模块
	 * @param func
	 */
	public void saveModule(Module module) {
		daoSupport.insertDomainObject(module);
	}
	
	/**
	 * 获取模块
	 * @param id
	 * @return
	 */
	public Map<String, Object> getModule(long id) {
		return daoSupport.getJdbcTemplate().queryForList("select * from sys_module where id = ?",
				new Object[]{id}).get(0);
	}
	
	/**
	 * 删除模块
	 * @param id
	 */ 
	public void deleteModule(long id) {
		daoSupport.getJdbcTemplate().update("delete from sys_module where parent_id=?", new Object[]{id});
		daoSupport.getJdbcTemplate().update("delete from sys_module where id=?", new Object[]{id});
	}
	
	/**
	 * 查询所有功能
	 * @param id
	 * @return
	 */
	public List<Function> getFunctionsByModuleId(long id) {
		return daoSupport.getJdbcTemplate().query("select * from sys_funtion where module_id=?",
				new BeanPropertyRowMapper<Function>(Function.class), id);
	}

	/**
	 * 保存功能
	 * @param func
	 */
	public void saveFunction(Function func) {
		daoSupport.insertDomainObject(func);
	}

	/**
	 * 获取指定功能
	 * @param id
	 * @return
	 */
	public Map<String, Object> getFunction(long id) {
		return daoSupport.getJdbcTemplate().queryForList("select * from sys_funtion where id = ?",
				new Object[]{id}).get(0);
	}
	
	/**
	 * 删除功能
	 * @param id
	 */
	public void deleteFunction(long id) {
		daoSupport.getJdbcTemplate().update("delete from sys_funtion where id=?", new Object[]{id});
	}
	
}
